package com.utils.basehelper.encrypt;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

import org.apache.commons.codec.digest.DigestUtils;

public class FileMd5 {

	public static void main(String[] args) {
//		C:\\Users\\XR-8\\Desktop\\202008241957SpeYUaqj.mp4
		System.out.println(getFileMD5("C:\\Users\\XR-8\\Desktop\\202008241957SpeYUaqj.mp4").toUpperCase());
	}
	
	/**
	 * 推荐此方法
	 * @param in
	 * @return
	 */
	public static String getFileMd5ByIn(InputStream in) {	
		
		try {
			return DigestUtils.md5Hex(in);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Get MD5 of one file:hex string,test OK!
	 * 这个方法会丢失字符 （最前面的0字符）
	 * 
	 * @param file
	 * @return
	 */
	public static String getFileMD5(File file) {
		if (!file.exists() || !file.isFile()) {
			System.out.println("1");
			return null;
		}
		MessageDigest digest = null;
		FileInputStream in = null;
		byte buffer[] = new byte[1024];
		int len;
		try {
			digest = MessageDigest.getInstance("MD5");
			in = new FileInputStream(file);
			while ((len = in.read(buffer, 0, 1024)) != -1) {
				digest.update(buffer, 0, len);
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		BigInteger bigInt = new BigInteger(1, digest.digest());
		return bigInt.toString(16);
	}

	/***
	 * Get MD5 of one file！test ok!
	 * 
	 * @param filepath
	 * @return
	 */
	public static String getFileMD5(String filepath) {
		File file = new File(filepath);
		return getFileMD5(file);
	}

	/**
	 * MD5 encrypt,test ok
	 * 
	 * @param data
	 * @return byte[]
	 * @throws Exception
	 */
	public static byte[] encryptMD5(byte[] data) throws Exception {

		MessageDigest md5 = MessageDigest.getInstance("MD5");
		md5.update(data);
		return md5.digest();
	}

	public static byte[] encryptMD5(String data) throws Exception {
		return encryptMD5(data.getBytes("utf-8"));
	}

	/***
	 * compare two file by Md5
	 * 
	 * @param file1
	 * @param file2
	 * @return
	 */
	public static boolean isSameMd5(File file1, File file2) {
		String md5_1 = FileMd5.getFileMD5(file1);
		String md5_2 = FileMd5.getFileMD5(file2);
		return md5_1.equals(md5_2);
	}

	/***
	 * compare two file by Md5
	 * 
	 * @param filepath1
	 * @param filepath2
	 * @return
	 */
	public static boolean isSameMd5(String filepath1, String filepath2) {
		File file1 = new File(filepath1);
		File file2 = new File(filepath2);
		return isSameMd5(file1, file2);
	}
}
